define(function () {
    return function ($compile, $scope) {
        require(
            ["PDUtilDir/dataService", "AppPathDir/finance/productFinanceInfos/support",

                "PDUtilDir/util", "PDUtilDir/inputSelect", "Date",
                "DateCN", "css!DateCss",
                "css!AppPathDir/finance/productFinanceInfos/views/css/css"],
            function (DataService, Support, Util, inputSelect) {
                /* 查询 start */
                $scope.search = {};
                $scope.cache = {
                    types: new Array(),
                    typeMap: new Map(),
                    saleStatus: new Array(),
                    saleStatusMap: new Map(),
                    provinces: new Array()
                };
                $scope.searchSupport = {
                    run: function () {
                        $scope.grid.reload({
                            queryParam: {
                                model: JSON.stringify($scope.search)
                            }
                        })
                    }
                };
                //获取类型信息
                $.ajax({
                    url: getServer() + "/flea/hclv/getTourism.htmls",
                    success: function (data) {
                        if (data.status != 200) {
                            return;
                        }
                        $scope.cache.types = data.data;
                        $.each(data.data, function (index, row) {
                            $scope.cache.typeMap.set(row.dictCode, row.dictName);
                        });
                        $scope.renderGridFun();
                    }
                });
                //获取销售状态
                $.ajax({
                    url: getServer() + "/flea/hclv/getSaleStatus.htmls",
                    success: function (data) {
                        if (data.status != 200) {
                            return;
                        }
                        $scope.cache.saleStatus = data.data;
                        $.each(data.data, function (index, row) {
                            $scope.cache.saleStatusMap.set(row.dictCode, row.dictName);
                        });
                        $scope.renderGridFun();
                    }
                });
                //获取省列表
                $.ajax({
                    url: getServer() + "/flea/hclv/getProvinces.htmls",
                    success: function (data) {
                        if (data.status != 200) {
                            return;
                        }
                        $scope.cache.provinces = data.data;
                        init_startPlaceProvince();
                        init_endPlaceProvince();
                    }
                });
                //获取城市信息
                $scope.getCity = function (type, pcode) {
                    $.ajax({
                        url: getServer() + "/flea/hclv/getCity.htmls",
                        data: {
                            'pcode': pcode
                        },
                        success: function (data) {
                            if (data.status != 200) {
                                return;
                            }
                            var citys = data.data;
                            if (type == "start") {
                                $scope.cache.startPlaceCity = data.data;
                                init_startPlaceCity();
                            } else {
                                $scope.cache.endPlaceCity = data.data;
                                init_endPalceCity();
                            }
                        }
                    });
                };
                //出发省下拉框初始化
                var init_startPlaceProvince = function () {
                        // 初始化下拉框
                        inputSelect.dropCache();
                        inputSelect({
                            id: "startPlaceProvince",
                            key: {
                                id: "dictCode",
                                name: "dictName",
                                data: "data",
                            },
                            data: $scope.cache.provinces,
                            simpleData: true,
                            // 启用文本搜索
                            // initData:[initProvice],
                            searchAble: false,
                            callback: function (id) {
                                var id = id[0];
                                $scope.search.startPlaceProvince = id;
                                // console.info($scope.search.startPlaceProvince);
                                $scope.getCity('start', id);
                            }
                        });
                    },
                    //出发城市下拉框初始化
                    init_startPlaceCity = function () {
                        // 初始化下拉框
                        inputSelect.dropCache();
                        inputSelect({
                            id: "startPlaceCity",
                            key: {
                                id: "dictCode",
                                name: "dictName",
                                data: "data",
                            },
                            data: $scope.cache.startPlaceCity,
                            simpleData: true,
                            // 启用文本搜索
                            // initData:[initProvice],
                            searchAble: false,
                            callback: function (id) {
                                var id = id[0];
                                $scope.search.startPlaceCity = id;
                            }
                        });
                    },
                    //目的省下拉框初始化
                    init_endPlaceProvince = function () {
                        // 初始化下拉框
                        inputSelect.dropCache();
                        inputSelect({
                            id: "endPlaceProvince",
                            key: {
                                id: "dictCode",
                                name: "dictName",
                                data: "data",
                            },
                            data: $scope.cache.provinces,
                            simpleData: true,
                            // 启用文本搜索
                            // initData:[initProvice],
                            searchAble: false,
                            callback: function (id) {
                                var id = id[0];
                                $scope.search.endPlaceProvince = id;
                                $scope.getCity('end', id);
                            }
                        });
                    },
                    //目的城市下拉框初始化
                    init_endPalceCity = function () {
                        // 初始化下拉框
                        inputSelect.dropCache();
                        inputSelect({
                            id: "endPalceCity",
                            key: {
                                id: "dictCode",
                                name: "dictName",
                                data: "data",
                            },
                            data: $scope.cache.endPlaceCity,
                            simpleData: true,
                            // 启用文本搜索
                            // initData:[initProvice],
                            searchAble: false,
                            callback: function (id) {
                                var id = id[0];
                                $scope.search.endPalceCity = id;
                            }
                        });
                    };
                // 类别下拉框初始化
                inputSelect.dropCache();
                inputSelect({
                    id: "productType",
                    key: {
                        id: "dictCode",
                        name: "dictName",
                        data: "data",
                    },
                    data: [{
                        "dictCode": "self",
                        "dictName": "自营"
                    }, {
                        "dictCode": "unself",
                        "dictName": "非自营"
                    }],
                    simpleData: true,
                    // 启用文本搜索
                    // initData:[initProvice],
                    searchAble: false,
                    callback: function (id) {
                        var id = id[0];
                        $scope.search.productType = id;
                    }
                });
                //出发日期初始化
                $("#departureDate").datetimepicker({
                    // 设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                    language: "cn",
                    // 输出格式化
                    // format : 'yyyy-mm-dd hh:ii:ss',
                    format: 'yyyy-mm-dd',
                    // 直接选择‘今天’
                    todayBtn: true,
                    // 设置最精确的时间选择视图
                    minView: 'month',
                    // 高亮当天日期
                    todayHighlight: true,
                    // 显示“上午”“下午”
                    showMeridian: true,
                    // 选择完毕后自动关闭
                    autoclose: true
                });
                /* 查询 end */

                // 班期
                $scope.timetableSupport = {
                    run: function (id) {

                        var allData = $scope.grid.getAllData(), data = undefined;
                        $.each(allData, function (index, row) {
                            if (row.id == id) {
                                data = row;
                                DataService.setObject('product.object', data);
                            }
                        });

                        window.top.tabs
                            .addTab({
                                id: id,
                                title: "【" + data.name + "】班期管理",
                                remote: {
                                    url: getServer()
                                    + "/static/core/page/home/subHome.jsp",
                                    iframe: true,
                                    param: {
                                        html: getServer()
                                        + "/static/app/finance/productFinanceInfos/views/timetable.html",
                                        ctrl: getServer()
                                        + "/static/app/finance/productFinanceInfos/timetable.js",
                                        id: id
                                    }
                                }
                            });
                    }
                };
                //列表初始化
                $scope.renderGridFun = function () {
                    if ($scope.cache.saleStatus.length > 0 && $scope.cache.types.length > 0) {
                        $scope.grid = Support.init($scope, $scope.searchSupport);
                    }
                }
            })
    }
});